package com.passion.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

import static com.passion.utils.DateTimeUtil.datetimeToDate;

/**
 * @program offline-e-commerce-data-warehouse
 * @Description 解析json数据
 * @Author limeihua
 * @Date 2024/10/02 18:23
 * @Version 1.0
 */
public class ParseJsonUtil {
    public static JSONObject JsonArrayToJsonObject(String jsonArrayStr, String categoriesKey, String categoriesValue) {
        String[] split = categoriesValue.split(",");
        JSONArray jsonArray = JSON.parseArray(jsonArrayStr);
        JSONObject newJson = new JSONObject();
        JSONArray categories = new JSONArray();
        JSONArray jsonArray1 = new JSONArray();
        int count = 0;
        for (String s : split) {
            String[] split1 = s.split(":");
            JSONObject series = new JSONObject();
            JSONArray data = new JSONArray();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject item = jsonArray.getJSONObject(i);
                if(count == 0){
                    categories.add(item.getString(categoriesKey));
                }
                data.add(item.getInteger(split1[0]));
            }
            newJson.put("categories", categories);
            series.put("name", split1[1]);
            series.put("data", data);
            jsonArray1.add(series);
            count++;
        }
        newJson.put("series",jsonArray1);
        return newJson;
    }
}

